package net.topcrazy.base.util;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 配置文件初始化
 * @author jh
 * 
 * Dec 29, 2009 4:52:31 PM
 */
public class PropertiesFileManage {
    private static final Log _log = LogFactory.getLog(PropertiesFileManage.class);
    private static String socketServerAddress = null;
    private static Map<String,String> socketClientAddress = null;
    private static final String FILE_NAME="mina";

    /**
     * 初始化服务端地址
     * @return
     * @author jh
     */
    public synchronized static String initMina() {
	if (socketServerAddress == null) {
	    ResourceBundle rb = ResourceBundle.getBundle(FILE_NAME, Locale.CHINA);
	    socketServerAddress = rb.getString("socket.mainserver.address");
	    _log.debug("[socket]开始初始化服务端地址："+socketServerAddress);
	}
	return socketServerAddress;
    }
    
    /**
     * 初始化客户端地址
     * @return
     * @author jh
     */
    public synchronized static Map<String,String> getClientServer() {
	if (socketClientAddress == null) {
	    socketClientAddress=new HashMap<String,String>();
	    ResourceBundle rb = ResourceBundle.getBundle(FILE_NAME, Locale.CHINA);
	    String address = rb.getString("socket.client.address");
	    if (address.endsWith(".*")) {
		String[] addresses=StringUtils.split(address, ",");
		for(int j=0;j<addresses.length;j++){
		    String addressPrefix = addresses[j].substring(0, addresses[j].length() - 1);
		    	for(int i=1; i<255 ; i++){
		    	    socketClientAddress.put(addressPrefix+i, addressPrefix+i);
		    	} 
		}
	    }
	    else {
	    	String[] cilentAddress = address.split(",");
	    	for(int i=0;i<cilentAddress.length;i++){
	    		socketClientAddress.put(cilentAddress[i], cilentAddress[i]);
	    		_log.debug("[socket]开始初始化客户端端地址："+socketClientAddress);
	    	}
	    }
		}
		return socketClientAddress;
    }

}
